package com.ljsw.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.ljsw.server.UpServer;
import com.ljsw.util.BitmapUtil;

public class ImageActivity extends Activity implements OnClickListener{
	private Button btn;
	private byte[] bytes;
	private String str;
	private Handler handler;
	private ImageView image;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		btn = (Button) this.findViewById(R.id.btndown);
		image = (ImageView) this.findViewById(R.id.downImage);
		btn.setOnClickListener(this);
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case 1:
					Toast.makeText(ImageActivity.this, "SUCCESS", Toast.LENGTH_LONG).show();
					Bitmap map = BitmapUtil.stringToBitmap(str);
					map = BitmapUtil.compressBmpFromBmp(map);
					image.setImageBitmap(map);
					
					break;

				case 2:
					Toast.makeText(ImageActivity.this, "ERROR", Toast.LENGTH_LONG).show();
					break;
				}
			}
		};
	}

	public void onClick(View arg0) {
		new Thread(new Runnable() {
			public void run() {
				try {
				//	bytes = UpServer.upImgShow("D:/Yupload/u123.jpg");
					str = UpServer.upImgShow("D:/userimg/u123.jpg");
					
					System.out.println("str:="+str);
					handler.sendEmptyMessage(1);
					
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendEmptyMessage(2);
				}
				
			}
		}).start();
		
	}
}
